package com.example.classcommunity3.service;

import com.alibaba.fastjson.JSON;
import com.example.classcommunity3.dao.SubmitHomeworkDao;
import com.example.classcommunity3.model.Homework;
import com.example.classcommunity3.model.Student;
import com.example.classcommunity3.model.SubmitHomework;

import com.sun.org.apache.xpath.internal.operations.Bool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

@Service
public class SubmitHomeworkService {

    @Autowired
    SubmitHomeworkDao submitHomeworkDao;

    @Autowired
    StudentService studentService;

    @Autowired
    HomeworkService homeworkService;

    public boolean isSubmitEmpty(Long hId) {
        /**
         @describe 判断当前作业是否有人提交
         @param [hId]
         @return boolean
         @date 2018/6/8
         */
        List<SubmitHomework> submitList = submitHomeworkDao.findByHomeworkId(hId);
        if (submitList.size() != 0)
            return true;
        else
            return false;
    }

    public boolean deleteByHomeworkId(Long id) {
        /**
         @describe 删除作业下的所有学生提交的作业
         @param [id]
         @return java.lang.Boolean
         @date 2018/6/7
         */
        int i = submitHomeworkDao.deleteByHomeworkId(id);
        return true;
    }

    public String findAllSubmit(Long hId) {
        /**
         @describe 找到某门课所有交作业了的学生
         @param [hId]
         @return java.lang.String
         @date 2018/6/7
         */
        List<HashMap> hashMapList = new ArrayList<>();
        List<SubmitHomework> submitList = submitHomeworkDao.findByHomeworkId(hId);
        List<Student> stuList;

        for (int i = 0; i < submitList.size(); i++) {
            SubmitHomework submitHomework = submitList.get(i);
            Long stuAccount = submitHomework.getsStuAccount();
            String stuName = studentService.getStuName(stuAccount);

            HashMap<String, Object> hashMap = new HashMap<>();

            hashMap.put("stuName", stuName);
            hashMap.put("stuAccount", stuAccount);
            hashMap.put("sItem", submitHomework.getsItem());
            hashMap.put("sTime", submitHomework.getsTime());

            //若文件名不为空
            if (submitHomework.getsFileUrl() != null) {
                hashMap.put("sFileUrl", submitHomework.getsFileUrl());
                hashMap.put("sFileName", submitHomework.getsFileName());
            } else {
                hashMap.put("sFileUrl", "");
                hashMap.put("sFileName", "");
            }


            hashMapList.add(hashMap);
        }
        String json = JSON.toJSONString(hashMapList);

        return json;
    }

    public boolean findHaveSubmit(Long hId, Long account) {
        SubmitHomework submitHomework = submitHomeworkDao.findByHomeworkIdAndSStuAccount(hId, account);

        if (submitHomework != null) {
            return true;
        }
        return false;
    }

    public boolean submitHomeworkWithFile(String url, String fileName, Long homeworkId, Long stuAccount, String item, String submitTime) {
        /**
         @describe 提交作业（有文件）
         @param [url, homeworkId, stuAccount, item, submitTime]
         @return boolean
         @date 2018/6/8
         */
        SubmitHomework submitHomework = new SubmitHomework();
        submitHomework.setHomeworkId(homeworkId);
        submitHomework.setsItem(item);
        submitHomework.setsStuAccount(stuAccount);
        submitHomework.setsFileName(fileName);
        submitHomework.setsTime(submitTime);
        submitHomework.setsFileUrl(url);

        try {
            submitHomeworkDao.save(submitHomework);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean submitHomeworkWithoutFile(Long homeworkId, Long stuAccount, String item, String submitTime) {
        /**
         @describe 提交作业（无文件）
         @param [homeworkId, stuAccount, item, submitTime]
         @return java.lang.Boolean
         @date 2018/6/8
         */
        SubmitHomework submitHomework = new SubmitHomework();
        submitHomework.setHomeworkId(homeworkId);
        submitHomework.setsItem(item);
        submitHomework.setsStuAccount(stuAccount);
        submitHomework.setsFileName("");
        submitHomework.setsFileUrl("");
        submitHomework.setsTime(submitTime);

        try {
            submitHomeworkDao.save(submitHomework);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean zipFiles(File[] srcfile, File zipfile) {
        /**
         @describe 压缩多个文件为一个zip压缩包
         @param [srcfile 源文件列表, zipfile 压缩后的文件]
         @return void
         @date 2018/6/9
         */
        byte[] buf = new byte[1024];
        try {

            ZipOutputStream out = new ZipOutputStream(new FileOutputStream(zipfile));
            for (int i = 0; i < srcfile.length; i++) {
                FileInputStream in = new FileInputStream(srcfile[i]);
                out.putNextEntry(new ZipEntry(srcfile[i].getName()));
                int len;
                while ((len = in.read(buf)) > 0) {
                    out.write(buf, 0, len);
                }
                out.closeEntry();
                in.close();
            }
            out.close();
            System.out.println("压缩完成.");
            return true;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return false;
        }
    }

    public String findZipFile(Long hId) {
        List<SubmitHomework> submitHomeworkList = submitHomeworkDao.findByHomeworkId(hId);
        List<File> fileList = new ArrayList<>();

        String homeworkTitle = homeworkService.findHomeworkTitleByHId(hId);
        String zipFileName = "homework.zip";
        String zipUrl = "D:\\submitHomework\\zip\\" + zipFileName;

        for (int i = 0; i < submitHomeworkList.size(); i++) {
            SubmitHomework submitHomework = submitHomeworkList.get(i);

            String url = submitHomework.getsFileUrl();
            String fileName = submitHomework.getsFileName();

            File file = new File(url + "\\" + fileName);
            fileList.add(file);
        }
        File[] srcFile = fileList.toArray(new File[0]);
        File zipFile = new File(zipUrl);

        if (zipFiles(srcFile, zipFile)) {
            return zipFileName;
        } else {
            return "";
        }
    }

    public boolean disSubmit(Long hId, Long account){
        int i;
        i= submitHomeworkDao.deleteByHomeworkIdAndSStuAccount(hId,account);

        return true;
    }

//    public boolean deleteSubmit(String subCode){
//
//    }
}
